package top.infopub.mgr.bpa.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.infopub.dto.bpa.BpaPublicProInfoDto;
import top.infopub.dto.bpa.SettlePlanDetailDto;
import top.infopub.dto.bpa.SettlePlanListDto;
import top.infopub.enums.bpa.BpaProcessDetailTypeEnum;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.enums.mkt.ProjectFileTypeEnum;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.bpa.dao.BpaSettlePlanDao;
import top.infopub.mgr.bpa.jsonVo.SettlePlanDetailJsonVo;
import top.infopub.mgr.bpa.service.BpaProcessManageService;
import top.infopub.mgr.bpa.service.BpaSettlePlanService;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.service.OAFlowService;
import top.infopub.mgr.mkt.dao.ProjectAttachmentDao;
import top.infopub.mgr.mkt.service.ProjectInfoService;
import top.infopub.mgr.mkt.service.SetupProService;
import top.infopub.mgr.util.FileAttrUtils;
import top.infopub.model.bpa.SettlePlan;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.util.IdFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 结算策划
 */
@Service
public class BpaSettlePlanServiceImpl extends GenericServiceImpl<SettlePlan, String>  implements BpaSettlePlanService {

    @Autowired
    private BpaSettlePlanDao bpaSettlePlanDao;

    @Autowired
    private ProjectAttachmentDao projectAttachmentDao;

    @Autowired
    private SetupProService setupProService;

    @Autowired
    private OAFlowService oaFlowService;

    @Autowired
    private BpaProcessManageService bpaProcessManageService;

    @Autowired
    private ProjectInfoService projectInfoService;


    @Override
    public Page<SettlePlanListDto> selectByDomainWhere(Page<SettlePlanListDto> page, SettlePlan settlePlan) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        Page<SettlePlanListDto> pageList = new Page<SettlePlanListDto>();
        pageList = bpaSettlePlanDao.selectListByDomainWhere(settlePlan);
        return pageList;
    }

    @Override
    public SettlePlanDetailJsonVo getDetail(String id) {
        //判断id是否为空 如果为空则表示是新增 不为空表示是详情页面
        SettlePlanDetailJsonVo settlePlanDetailJsonVo = new SettlePlanDetailJsonVo();
        if(StringUtils.isBlank(id)){
            settlePlanDetailJsonVo = addDetailReturnData(settlePlanDetailJsonVo);
        }else {
            settlePlanDetailJsonVo = detailReturnData(id, settlePlanDetailJsonVo);
        }
        return settlePlanDetailJsonVo;
    }

    @Override
    public RspVo save(SettlePlanDetailJsonVo detailJson, Integer submitType, AuthorizationVO vo) {

        SettlePlanDetailDto settlePlanDetailDto = detailJson.getInfo();
        SettlePlan settlePlan = new SettlePlan();
        settlePlan.setUptBy(vo.getId());
        settlePlan.setUptTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
        if(StringUtils.isBlank(detailJson.getInfo().getId())){
            //新增
            String id = IdFactory.getUUIDUpper();
            settlePlan.setId(id);
            settlePlan.setAddBy(vo.getId());
            settlePlan.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
            settlePlan.setProCode(settlePlanDetailDto.getProCode());
            settlePlan.setCodes("");
            settlePlan.setProcessId(IdFactory.getUUIDUpper());
            settlePlan.setProcessStatus("1");
            settlePlan.setMemo("");
            settlePlan.setOaId("");
            bpaSettlePlanDao.addSettlePlanInfo(settlePlan);
        }else{
            //校验商业策划数据是否存在
            SettlePlan settlePlanVo = bpaSettlePlanDao.queryInfoById(settlePlanDetailDto.getId());
            if (settlePlanVo == null) return RspVo.getFailureResponse(400, "商业策划详情不存在");
            //判断审批状态
            if (StringUtils.isNotBlank(settlePlanVo.getProcessStatus())
                    && (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), settlePlanVo.getProcessStatus())
                    || StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS9.getCode(), settlePlanVo.getProcessStatus()))) {
                return RspVo.getFailureResponse(400, "审批状态不正确，操作失败");
            }
            //更新
            bpaSettlePlanDao.updateByIdSelective(settlePlan);
            settlePlan.setOaId(settlePlan.getOaId());//预留给OA流程id
        }

        //更新附件表-MKT_PROJECT_ATTACHMENT
        List<ProjectAttachment> attachmentList = new ArrayList<>();
        if(detailJson.getFileAtt()!=null){
            String[] fileAtt = detailJson.getFileAtt();
            for (String file : fileAtt) {
                ProjectAttachment attachment = FileAttrUtils.createProjectFileAtta(settlePlan.getId(), file);
                attachmentList.add(attachment);
            }
        }
        projectAttachmentDao.deleteByWhere(settlePlan.getId(), ProjectFileTypeEnum.FILE_TYPE138.getCode(), null);
        if (attachmentList != null && attachmentList.size() > 0) {
            projectAttachmentDao.insertBatch(attachmentList);
        }

        //提交流程
        if (1 == submitType) {
            SettlePlan settlePlanVo = bpaSettlePlanDao.queryInfoById(settlePlan.getId());
            if (StringUtils.isNotBlank(settlePlanVo.getOaId())) {
                oaFlowService.finishOA(settlePlanVo.getOaId());
            }
            SettlePlanDetailJsonVo settlePlanDetailJsonVo = getDetail(settlePlan.getId());
            String json = JSON.toJSONString(settlePlanDetailJsonVo);
            bpaProcessManageService.sendProcess(BpaProcessDetailTypeEnum.PRO_NODE_TYPE26, json, settlePlan.getId(), settlePlan.getProCode(), vo);
        }
        return  RspVo.getSuccessResponse();
    }

    //新增的数据封装返回
    public SettlePlanDetailJsonVo addDetailReturnData(SettlePlanDetailJsonVo settlePlanDetailJsonVo){
        //返回所有的项目code和name
        settlePlanDetailJsonVo.setProInfoList(setupProService.queryPublicProInfo());
        return settlePlanDetailJsonVo;
    }


    @Override
    public GenericDao<SettlePlan, String> getDao() {
        return bpaSettlePlanDao;
    }


    //查看详情的数据返回
    public SettlePlanDetailJsonVo detailReturnData(String id, SettlePlanDetailJsonVo settlePlanDetailJsonVo){

        SettlePlan settlePlan = bpaSettlePlanDao.queryInfoById(id);

        //项目的基础信息
        BpaPublicProInfoDto  dto = projectInfoService.queryBpaProInfoByProCode(settlePlan.getProCode());
        SettlePlanDetailDto info = new SettlePlanDetailDto();
        info.setId(id);
        info.setApplyCompName(dto.getApplyCompName());
        info.setApplyDeptName(dto.getApplyDeptName());
        info.setApplyStaffName(dto.getApplyStaffName());
        info.setAreaCompName(dto.getAreaCompName());
        info.setCoopName(dto.getCoopName());
        info.setProCode(dto.getProCode());
        info.setProName(dto.getProName());
        info.setProCompName(dto.getProCompName());
        info.setProStaffName(dto.getProStaffName());
        info.setSgCityName(dto.getSgCityName());
        info.setProStaffName(dto.getProStaffName());
        settlePlanDetailJsonVo.setInfo(info);

        //下拉列表的数据
//        List<Map<String,String>> listMap = new ArrayList<>();
//        Map<String,String> map = new HashMap<>();
//        map.put(dto.getProCode(), dto.getProName());
//        listMap.add(map);
//        settlePlanDetailJsonVo.setProNameAndId(listMap);

        //附件信息表
        List<ProjectAttachment> projectAttaList = projectAttachmentDao.queryByWhere(id, ProjectFileTypeEnum.FILE_TYPE132.getCode(), null);
        settlePlanDetailJsonVo.setFileAtt(setupProService.fileAttaListToArray(projectAttaList));

        return settlePlanDetailJsonVo;
    }

}
